package com.longjianghuasheng.logistics.business.mine.viewmodel

import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.network.apiService

class ChangeViewModel : BaseMultiStateViewModel() {
    val startCount by lazy {
        ObservableInt()
    }
    val mobileObservableField by lazy {
        ObservableField("")
    }

    val codeObservable by lazy {
        ObservableField("")
    }

    val notify by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    fun onGetCode() {

        val mobile = mobileObservableField.get()
        if (!checkPhone(mobile)) return
        commit({ apiService.get_code(mobile ?: "", 1) }, {
            startCount.set(60)
        })

    }

    fun change_mobile() {

        val mobile = mobileObservableField.get()
        val code = codeObservable.get()
        if (!checkPhone(mobile)) return
        if (code.isNullOrBlank()) {
            toastNormal("验证码不能为空")
            return
        }
        commit2({ apiService.change_mobile(mobile ?: "", code) }, notify)
    }


}